/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR_item.dta", clear // open dataset

* Sample
keep if sample_int==1 & eis_f!=. // keep observations of interest


* ---------
* Table A18
* ---------

* Globals
global dep "ln_up_i c1_dlog_up ln_up_f c1_dlog_w htrl_f_int"
global controls "tax_rate risk red_i mixed_dec valitrade_advice"

* Matrix
mat tableA18 = J(10,5,.)
mat tableA18_T = J(1,5,.)

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep {
	
	qui: reghdfe `x' eis_f $controls countryorig* if sample_reg==1 , absorb(ihs key_dec ym_*) vce(cluster insp_f key_dec)
	
	mat tableA18[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat tableA18[`i',`j'] = _se[eis_f]
	mat tableA18_T[1,`j'] = e(N)
	local i = `i' + 8
	mat tableA18[`i',`j'] = e(r2)
	local i = `i' -9
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table A18.xlsx", replace sheet(TableA18) // create a new excel spreadsheet

* Coefficients
putexcel B4 = matrix(tableA18), nformat(0.000) hcenter vcenter
putexcel B12 = matrix(tableA18_T), nformat(number_sep) hcenter vcenter

* Title
putexcel (A1:F1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* Column names
putexcel A2 = "Dependent variable", left vcenter
local cells `" "B" "C" "D" "E" "F" "'
local labels `" "Log initial unit price" "\Delta log unit price" "Log final unit price" "\Delta log weight" "Hyp. tax revenue losses" "'
forvalues i = 1(1)5 {
	
	local x: word `i' of `labels'
	local y: word `i' of `cells'
	
	putexcel `y'2 = "`x'", hcenter vcenter bold	
	putexcel `y'3 = "(`i')", hcenter vcenter
	
	forvalues j = 6(1)11 {
		putexcel `y'`j' = "Yes", hcenter vcenter
	}
}

* Row names
putexcel A4 = "Excess interaction share", left vcenter
local j = 6
local names `" "Inspector-HS 8-digit product fixed effects" "Declaration characteristics" "Item characteristics" "Broker fixed effects" "Source country fixed effects" "Month-year fixed effects" "'
forvalues i = 1(1)6 {
	
	local x: word `i' of `names'
	
	putexcel A`j' = "`x'", left vcenter
	local j = `j'+1
}
putexcel A12 = "Observations", left vcenter
putexcel A13 = "R-squared", left vcenter


* -------------------------------- ENDS HERE -------------------------------- *